<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Redis;

class Adminislogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $admintoken = $request->header('admintoken');
        $memberinfo = Redis::get($admintoken);

        $memberinfo = $memberinfo ? json_decode($memberinfo, true) : '';

        if (!is_array($memberinfo)) {
            return response()->json([
                'code' => 401, 'msg' => '登录失效，请重新登录!'
            ], 200);
        } else {

            //判断token是否存在于登录token集合
            if (isset($memberinfo['id'])) {
                $admintokenbyid = Redis::get('admin_user_admintokenbyid_' . $memberinfo['id']);
                if ($admintoken!=$admintokenbyid) {
                    return response()->json([
                        'code' => 401, 'msg' => '登录失效，请重新登录!!'
                    ], 200);
                    // return response()->json([
                    //     'code' => 401, 'msg' => $admintokenbyid.'登录失效，请重新登录!!.'.$admintoken
                    // ], 401);
                }
            } else {
                return response()->json([
                    'code' => 401, 'msg' => '登录失效，请重新登录'
                ], 200);
            }



            $res = $next($request);
            return $res;
        }
    }
}
